import 'package:app/views/ai/ai.dart';
import 'package:app/views/index/index.dart';
import 'package:app/views/notice/notice.dart';
import 'package:app/views/user/user.dart';
import 'package:creato_ui/creato_ui.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final Map<String, IconData> _iconsMap = {
    "消息": Icons.message,
    "工作台": Icons.folder,
    "AI": Icons.adb,
    "我的": Icons.people,
  }; // 底部Tab栏数据

  late PageController _pageController; // PageView控制器

  int _currentIndex = 3;

  @override
  void initState() {
    _pageController = PageController(initialPage: _currentIndex)
      ..addListener(() {
        setState(() {
          _currentIndex = _pageController.page?.toInt() ?? 0;
        });
      });

    super.initState();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageController,
        children: [
          // CreatoUIKeepAliveWidget(child: NoticePage()),
          // CreatoUIKeepAliveWidget(child: IndexPage()),
          // CreatoUIKeepAliveWidget(child: AIPage()),
          // CreatoUIKeepAliveWidget(child: UserPage()),
          NoticePage(),
          IndexPage(),
          AIPage(),
          UserPage(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          unselectedItemColor: Colors.grey,
          currentIndex: _currentIndex,
          selectedItemColor: Colors.blue,
          onTap: (value) => setState(() {
                _currentIndex = value;
                _pageController.jumpToPage(value);
              }),
          items: _iconsMap.keys
              .map((key) => BottomNavigationBarItem(
                  label: key, icon: Icon(_iconsMap[key])))
              .toList()),
    );
  }
}
